{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from glob import glob\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def image_to_string(path):\n",
    "    url = 'http://19z8857a58.iok.la:88/archives/capi/ocr.do'\n",
    "    data = {'userImageFileName': '0123456789_kk.jpg'}\n",
    "    files = {'file': open(path, 'rb')}\n",
    "    res = requests.post(url, data=data, files=files)\n",
    "    txt = ''\n",
    "    try:\n",
    "        txt = json.loads(res.text).get('result', '')\n",
    "    except: pass\n",
    "    return txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\r\\n\\ufeff霭F一．，\\r\\n娜袱︸…、\\r\\n琊耱览一\\r\\n～‘一矗——瓤呗\\r\\n＼户'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image_to_string('./imgs/test/1/0068a82e-881e-480b-8b89-f1283e7fd86d.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import jieba"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with open('./chi_sim.txt') as f:\n",
    "    chi_sim = [w[0] for w in f.readlines()[:3500]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def dumpText(dirname, txtname, chi_sim):\n",
    "    with open(txtname, 'w') as f:\n",
    "        paths = glob(os.path.join(dirname, '*'))\n",
    "        for path in tqdm(paths):\n",
    "            txt = image_to_string(path)\n",
    "            txt = ''.join([w for w in txt if w in chi_sim + ['\\n']])\n",
    "            txt = txt.replace('\\n', ' ')\n",
    "            seg = ' '.join(jieba.lcut(txt))\n",
    "            seg = ' '.join(seg.split())\n",
    "            f.write(seg + '\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "dumpText('imgs/train/0/', 'imgs/train/0.txt', chi_sim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  3%|▎         | 2/70 [00:00<00:24,  2.78it/s]Building prefix dict from the default dictionary ...\n",
      "Loading model from cache /tmp/jieba.cache\n",
      "Loading model cost 1.771 seconds.\n",
      "Prefix dict has been built succesfully.\n",
      "100%|██████████| 70/70 [01:24<00:00,  1.20s/it]\n"
     ]
    }
   ],
   "source": [
    "dumpText('imgs/train/1/', 'imgs/train/1.txt', chi_sim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 70/70 [03:24<00:00,  2.92s/it]\n"
     ]
    }
   ],
   "source": [
    "dumpText('imgs/test/0/', 'imgs/test/0.txt', chi_sim)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 70/70 [03:12<00:00,  2.75s/it]\n"
     ]
    }
   ],
   "source": [
    "dumpText('imgs/test/1/', 'imgs/test/1.txt', chi_sim)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
